package com.learn.xml_config.basic_config;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class XMLConfig {
    public static void main(String[] args) {
        // id/class/name：观察getBean参数
        propertyIdClassName();
        System.out.println("===============");
        
        // scope：观察Bean实例化时机，以及Bean对象地址值
        propertyScope();
        System.out.println("===============");
        
        // lazy-init：观察Bean实例化时机，以及Bean对象地址值
        propertyLazyinit();
        System.out.println("===============");
        
        // init-method/destroy-method：观察init和destroy输出。没有执行destroy是因为Spring没有执行到关闭容器的步骤程序就停止了，需要手动关闭容器执行destroy
        propertyInitmethodDestroymethod();
        System.out.println("===============");
        
        // factory-bean/factory-method
        propertyFactorybeanFactorymethod();
        System.out.println("===============");
        
        // autowire：改变xml中autowire和id值观察输出
        propertyAutowire();
    }
    
    public static void propertyIdClassName() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("xml-config/basic-config/id-class-name.xml");
        
        System.out.println(applicationContext.getBean("userDao"));
        System.out.println(applicationContext.getBean("userDao(name-a)"));
        System.out.println(applicationContext.getBean("userDao(name-b)"));
        System.out.println(applicationContext.getBean("com.learn.xml_config.basic_config.beans.dao.UserDaoImpl"));
    }
    
    public static void propertyScope() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("xml-config/basic-config/scope.xml");
        
        System.out.println(applicationContext.getBean("userService(scope:singleton)"));
        System.out.println(applicationContext.getBean("userService(scope:singleton)"));
        System.out.println(applicationContext.getBean("userService(scope:prototype)"));
        System.out.println(applicationContext.getBean("userService(scope:prototype)"));
    }
    
    public static void propertyLazyinit() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("xml-config/basic-config/lazy-init.xml");
        
        System.out.println(applicationContext.getBean("userService(lazy-init:false)"));
        System.out.println(applicationContext.getBean("userService(lazy-init:false)"));
        System.out.println(applicationContext.getBean("userService(lazy-init:true)"));
        System.out.println(applicationContext.getBean("userService(lazy-init:true)"));
    }
    
    public static void propertyInitmethodDestroymethod() {
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("xml-config/basic-config/init-destroy-method.xml");
        
        applicationContext.getBean("userService");
        applicationContext.close();
    }
    
    public static void propertyFactorybeanFactorymethod() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("xml-config/basic-config/factory-bean-method.xml");
        
        System.out.println(applicationContext.getBean("userDao(factory:static)"));
        System.out.println(applicationContext.getBean("userDao(factory:instance)"));
        
        // singletonObject中键userDao(factory:impl)的值为FactoryBeanImpl，factoryBeanObjectCache中键userDao(factory:impl)的值为UserDaoImpl
        // 首次getBean时执行getObject返回相应对象，后续getBean时从缓存factoryBeanObjectCache中取
        System.out.println(applicationContext.getBean("userDao(factory:impl)"));
        System.out.println(applicationContext.getBean("userDao(factory:impl)"));
    }
    
    public static void propertyAutowire() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("xml-config/basic-config/autowire.xml");
    }
}
